package com.ruoyi.zhmk.service.impl;

import java.text.SimpleDateFormat;
import java.util.*;

import com.ruoyi.carApi.click.CarApiClick;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.zhmk.domain.ZhmkCarDevice;
import com.ruoyi.zhmk.domain.ZhmkPerson;
import com.ruoyi.zhmk.domain.request.SendCarRequest;
import com.ruoyi.zhmk.domain.response.SendCarResponse;
import com.ruoyi.zhmk.mapper.ZhmkCarDeviceMapper;
import com.ruoyi.zhmk.utils.CarApiUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.zhmk.mapper.ZhmkCarMapper;
import com.ruoyi.zhmk.domain.ZhmkCar;
import com.ruoyi.zhmk.service.IZhmkCarService;

/**
 * 常驻车辆Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-06-25
 */
@Service
public class ZhmkCarServiceImpl implements IZhmkCarService 
{
    @Autowired
    private ZhmkCarMapper zhmkCarMapper;
    @Autowired
    private ZhmkCarDeviceMapper zhmkCarDeviceMapper;

    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
    private SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");

    /**
     * 查询常驻车辆
     * 
     * @param id 常驻车辆ID
     * @return 常驻车辆
     */
    @Override
    public ZhmkCar selectZhmkCarById(Long id)
    {
        return zhmkCarMapper.selectZhmkCarById(id);
    }

    /**
     * 查询常驻车辆列表
     * 
     * @param zhmkCar 常驻车辆
     * @return 常驻车辆
     */
    @Override
    public List<ZhmkCar> selectZhmkCarList(ZhmkCar zhmkCar)
    {
        return zhmkCarMapper.selectZhmkCarList(zhmkCar);
    }

    /**
     * 新增常驻车辆
     * 
     * @param zhmkCar 常驻车辆
     * @return 结果
     */
    @Override
    public int insertZhmkCar(ZhmkCar zhmkCar)
    {
        zhmkCar.setCreateTime(DateUtils.getNowDate());
        return zhmkCarMapper.insertZhmkCar(zhmkCar);
    }

    /**
     * 修改常驻车辆
     * 
     * @param zhmkCar 常驻车辆
     * @return 结果
     */
    @Override
    public int updateZhmkCar(ZhmkCar zhmkCar)
    {
        return zhmkCarMapper.updateZhmkCar(zhmkCar);
    }

    /**
     * 批量删除常驻车辆
     * 
     * @param ids 需要删除的常驻车辆ID
     * @return 结果
     */
    @Override
    public int deleteZhmkCarByIds(Long[] ids)
    {
        //根据ids查找
        List<ZhmkCar> zhmkCarList = zhmkCarMapper.selectZhmkCarByIds(ids);
        List<ZhmkCarDevice> carDevices = zhmkCarDeviceMapper.selectZhmkCarDeviceList(new ZhmkCarDevice());
        for (ZhmkCarDevice carDevice : carDevices) {
            for (ZhmkCar zhmkCar : zhmkCarList) {
                CarApiClick carApiClick = CarApiUtil.getCarApiClick();
                carApiClick.deleteWhiteItem(carDevice.getIp(),zhmkCar.getPlateNumber());
            }
        }

        return zhmkCarMapper.deleteZhmkCarByIds(ids);
    }

    /**
     * 删除常驻车辆信息
     * 
     * @param id 常驻车辆ID
     * @return 结果
     */
    @Override
    public int deleteZhmkCarById(Long id)
    {
        ZhmkCar zhmkCar = zhmkCarMapper.selectZhmkCarById(id);
        if(zhmkCar == null){
            return 0;
        }
        List<ZhmkCarDevice> carDevices = zhmkCarDeviceMapper.selectZhmkCarDeviceList(new ZhmkCarDevice());
        for (ZhmkCarDevice carDevice : carDevices) {
            CarApiClick carApiClick = CarApiUtil.getCarApiClick();
            carApiClick.deleteWhiteItem(carDevice.getIp(),zhmkCar.getPlateNumber());

        }
        return zhmkCarMapper.deleteZhmkCarById(id);
    }

    /**
     * 车辆下发
     * @param sendCarRequest
     * @return
     */
    @Override
    public AjaxResult carSend(SendCarRequest sendCarRequest) {
        Map<Long,Integer> carMap = new HashMap<>();
        if(sendCarRequest.getDeviceIdList() == null || sendCarRequest.getDeviceIdList().size()<=0){
            throw new CustomException("参数错误", HttpStatus.BAD_REQUEST);
        }
        List<Long> deviceIdList = sendCarRequest.getDeviceIdList();
        List<SendCarResponse> sendCarResponseList = new ArrayList<>();
        for (Long aLong : deviceIdList) {
            ZhmkCarDevice zhmkCarDevice = zhmkCarDeviceMapper.selectZhmkCarDeviceById(aLong);
            String ip = zhmkCarDevice.getIp();
            //查询出来车辆列表
            List<Long> carIdList = sendCarRequest.getCarIdList();
            Long[] ids = new Long[carIdList.size()];
            List<ZhmkCar> zhmkCarList = zhmkCarMapper.selectZhmkCarByIds(carIdList.toArray(ids));
            CarApiClick carApiClick = CarApiUtil.getCarApiClick();
            int sendSuccessCount = 0;
            int sendFailCount = 0;
            for (ZhmkCar zhmkCar : zhmkCarList) {
                String plateNumber = zhmkCar.getPlateNumber();
                Date startTime = zhmkCar.getStartTime();
                Date endTime = zhmkCar.getEndTime();
                String startDateStr = dateFormat.format(startTime);
                String endDateStr = dateFormat.format(endTime);
                String startTimeStr = timeFormat.format(startTime);
                String endTimeStr = timeFormat.format(endTime);

                int i = carApiClick.addWhiteItem(ip, plateNumber, startDateStr, endDateStr, startTimeStr, endTimeStr, plateNumber, "", "", "");

                Integer integer = carMap.get(zhmkCar.getId());
                if(integer == null){
                    carMap.put(zhmkCar.getId(),0);
                    integer = 0;
                }
                if(i==0){
                    //失败
                    sendFailCount ++;

                }else{
                    //成功
                    sendSuccessCount ++;
                    carMap.put(zhmkCar.getId(),integer.intValue()+1);
                }

            }
            SendCarResponse sendCarResponse = new SendCarResponse();
            sendCarResponse.setSendFailCount(sendFailCount);
            sendCarResponse.setSendSuccessCount(sendSuccessCount);
            sendCarResponse.setZhmkCarDevice(zhmkCarDevice);
            sendCarResponseList.add(sendCarResponse);

        }
        //人员状态修改，所有设备下发成功后，才算下发成功
        for (Map.Entry<Long, Integer> longIntegerEntry : carMap.entrySet()) {
            Integer value = longIntegerEntry.getValue();
            Long key = longIntegerEntry.getKey();
            ZhmkCar zhmkCar = new ZhmkCar();
            zhmkCar.setId(key);
            if(value == deviceIdList.size()){
                //所有设备下发成功
                zhmkCar.setSendStatus(1);
            }else{
                //下发失败
                zhmkCar.setSendStatus(0);
            }
            zhmkCarMapper.updateZhmkCar(zhmkCar);
        }

        return AjaxResult.success(sendCarResponseList);
    }
}
